// pages/service/orderConfirm/orderConfirm.js
const common = require('../../../utils/util.js')
const app = getApp()
Page({

    /**
     * 页面的初始数据
     */
    data: {
        goodsInfo: {},
        shopInfo: {},
        orderMark: '',
        cartId: null,
        isLoading: false
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {

    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function() {
        this.getOrderInfo()
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function() {

    },

    getOrderInfo: function() {
        this.setData({
            goodsInfo: wx.getStorageSync('serviceGoodsInfo'),
            cartId: wx.getStorageSync('serviceCartId'),
            shopInfo: wx.getStorageSync('shopInfo')
        })
    },

    goPay: function() {
        const that = this
        if (this.data.isLoading) {
            return
        }
        this.setData({
            isLoading: true
        })
        wx.showLoading({
            title: '支付中'
        })
        wx.login({
            success(response) {
                if (response.code) {
                    //发起网络请求
                    let data = {
                        total_price: that.data.goodsInfo.self_price * that.data.goodsInfo.num,
                        breft: that.data.orderMark,
                        pay_id: 1,
                        pay_client: 1,
                        code: response.code,
                        cart_id_list: that.data.cartId,
                        is_goods: 4
                    }
                    common.post('Order/new_order', data).then(function(response) {
                        wx.hideLoading()
                        if (response.code == 204) {
                            wx.showToast({
                                title: response.error,
                                icon: 'none',
                                duration: 2000
                            })
                            that.setData({
                                isLoading: false
                            })
                            return
                        }
                        wx.requestPayment({
                            timeStamp: response.timeStamp,
                            nonceStr: response.nonceStr,
                            package: response.package,
                            signType: response.signType,
                            paySign: response.paySign,
                            success(res) {
                                that.setData({
                                    isLoading: false
                                })
                                wx.showLoading({
                                    title: '请稍后'
                                })
                                setTimeout(function() {
                                    wx.hideLoading()
                                    wx.redirectTo({
                                        url: '/pages/service/orderList/orderList?orderType=2'
                                    })
                                }, 2000)
                            },
                            fail(res) {
                                that.setData({
                                    isLoading: false
                                })
                                wx.showToast({
                                    title: '支付失败',
                                    icon: 'none',
                                    duration: 2000
                                })
                                wx.showLoading({
                                    title: '请稍后'
                                })
                                setTimeout(function () {
                                    wx.hideLoading()
                                    wx.redirectTo({
                                        url: '/pages/service/orderList/orderList?orderType=1'
                                    })
                                }, 2000)
                            }
                        })
                    }).catch(function(e) {})
                }
            }
        })
    }
})